package com.ctrip.demo.design.chain.loginchain;


public class Test {

    public static void main(String[] args) {

        Request build = new Request.RequestBuilder().frequentOk(true).loggedOn(true).build();

        RequestFrequentHandler requestFrequentHandler = new RequestFrequentHandler(new LoginHandler(null));
        if (requestFrequentHandler.process(build)) {
            System.out.println("验证成功");
        } else {
            System.out.println("网络异常");
        }

    }


}

abstract class Handler {

    private Handler next;

    public Handler(Handler next) {
        this.next = next;
    }

    public Handler getNext() {
        return next;
    }

    public void setNext(Handler next) {
        this.next = next;
    }

    abstract boolean process(Request request);
}


class Request {

    boolean loggedOn;

    boolean isFrequentOk;

    boolean isPermits;

    boolean containsSensitiveWords;

    private Request(boolean loggedOn, boolean isFrequentOk, boolean isPermits, boolean containsSensitiveWords) {
        this.containsSensitiveWords = containsSensitiveWords;
        this.loggedOn = loggedOn;
        this.isFrequentOk = isFrequentOk;
        this.isPermits = isPermits;
    }

    static class RequestBuilder {

        private boolean loggedOn;

        private boolean isFrequentOk;

        private boolean isPermits;

        private boolean containsSensitiveWords;

        RequestBuilder loggedOn(boolean loggedOn) {
            this.loggedOn = loggedOn;
            return this;
        }

        RequestBuilder frequentOk(boolean frequentOk) {
            this.isFrequentOk = frequentOk;
            return this;
        }

        RequestBuilder isPermits(boolean isPermits) {
            this.isPermits = isPermits;
            return this;
        }

        public Request build() {
            Request request = new Request(loggedOn, isFrequentOk, isPermits, containsSensitiveWords);
            return request;
        }

        public boolean isLoggedOn() {
            return loggedOn;
        }

        public boolean isFrequentOk() {
            return isFrequentOk;
        }

        public boolean isPermits() {
            return isPermits;
        }

        public boolean isContainsSensitiveWords() {
            return containsSensitiveWords;
        }
    }
}

class RequestFrequentHandler extends Handler {

    public RequestFrequentHandler(Handler next) {
        super(next);
    }

    @Override
    boolean process(Request request) {

        if (request.isFrequentOk) {
            System.out.println("访问频率的控制");
            Handler next = getNext();
            if (null == next) {
                return true;
            }
            if (!next.process(request)) {
                return false;
            } else {
                return true;
            }

        }
        return false;
    }
}

class LoginHandler extends Handler {

    public LoginHandler(Handler next) {
        super(next);
    }

    @Override
    boolean process(Request request) {

        if (request.loggedOn) {
            System.out.println("登录认证");
            Handler next = getNext();
            if (null == next) {
                return true;
            } else {
                if (!next.process(request)) {
                    return false;
                } else {
                    return true;
                }
            }
        }
        return false;
    }
}
